﻿using System.Collections.Generic;
using System.Linq;
using IGM.DataProvider;
using IGM.Framework.Constants;

namespace IGM.Business
{
    public partial class Business
    {
        #region -- Get KindIntern --

        /// <summary>
        /// This function shows list of Majors.
        /// </summary>
        /// <returns></returns>
        public List<KindIntern> GetListKindIntern()
        {
            return DataContex.KindInterns.ToList();
        }

        #endregion

        #region Common

        private static bool KindInterCommonValidator(KindIntern kindIntern, out string message)
        {
            if (string.IsNullOrWhiteSpace(kindIntern.Name))
            {
                message = string.Format(ConstantMessages.RequireFieldIsBlank, "tên Loại thực tập");
                return false;
            }

            if (kindIntern.Name.Length >= 50)
            {
                message = string.Format(ConstantMessages.FieldDataOverMaxLength, "tên Loại thực tập", 50);
                return false;
            }
            message = string.Empty;
            return true;
        }

        #endregion       

        #region -- Edit Kind Intern --

      
        public bool EditKindInterValidator(KindIntern kindIntern, out string message)
        {
            var id = kindIntern.Id;

            if (!KindInterCommonValidator(kindIntern, out message)) return false;

            if (DataContex.KindInterns.Any(k => k.Name.ToLower() == kindIntern.Name.ToLower() && k.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, kindIntern.Name);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="kindIntern"></param>
        /// <returns></returns>
        public bool EditKindIntern(KindIntern kindIntern)
        {
            var id = kindIntern.Id;
            var updateKindIntern = DataContex.KindInterns.SingleOrDefault(f => f.Id == id);

            if (updateKindIntern == null)
                return false;

            updateKindIntern.Name = kindIntern.Name;
            updateKindIntern.Year = kindIntern.Year;

            DataContex.SaveChanges();
            return true;
        }

        #endregion       
        
    }
}